package com.example.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.server.entity.Video;
import com.example.server.service.VideoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * (Video)表控制层
 *
 * @author makejava
 * @since 2022-11-21 09:10:56
 */
@RestController
@RequestMapping("video")
public class VideoController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private VideoService videoService;

    /**
     * 查询所有数据
     */
    @GetMapping("/selectall")
    public R selectAll() {
        R r =new R<>();
        List<Video> re = videoService.getBaseMapper().selectList(null);
        r.setCode(200);
        r.setData(re);
        r.setMsg("查询成功");
        return r;
    }

    //查询所有审核成功（或有待审核的）数据进行发布
    @GetMapping
    public R selectAlltrue() {
        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
//        queryWrapper.select("s");
        List<Video> re = videoService.getBaseMapper().selectList(null);
        List<Video> videoAdmit = new ArrayList<Video>();; //通过审核的video
        if(re.size()>0)
        {
            r.setCode(200);
            for(int i = 0 ; i < re.size() ; i++)
            {
                if(re.get(i).getAudit() == 1){
                    videoAdmit.add(re.get(i));
                }
            }
            r.setData(videoAdmit);
            r.setMsg("查询成功");
            return r;
        }
        r.setCode(404);
        r.setData(re);
        r.setMsg("查询失败");
        return r;
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.videoService.getById(id));
    }

    //根据userId来查询用户下所发布的所有视频
    @PostMapping("/selectByuserId/{userId}")
    public R selectByuserId(@PathVariable Serializable userId) {
        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<Video> myvideo = videoService.getBaseMapper().selectList(queryWrapper);
            r.setCode(200);
            r.setData(myvideo);
            r.setMsg("执行成功");
            return r;
    }
    /**
     * 新增数据
     * 视频投稿提交
     */
    @PostMapping("/submit")
    public R insert(@RequestBody Video video) {
        R r=new R<>();
        Date date= Calendar.getInstance().getTime();
        Format fmt=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        video.setCreatetime(fmt.format(date));
        System.out.println(video);
        this.videoService.save(video);
        r.setCode(200);
        r.setMsg("提交成功");
        return r;
    }

    /**
     * 修改数据
     *
     * @param video 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Video video) {
        return success(this.videoService.updateById(video));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.videoService.removeByIds(idList));
    }
}

